package com.bridgeintelligent.tag.webserver.system.controller;

import com.bridgeintelligent.tag.constants.PublicConstant;
import com.bridgeintelligent.tag.constants.Resp;
import com.bridgeintelligent.tag.webserver.system.exception.SystemErrors;
import com.bridgeintelligent.tag.webserver.system.model.NoticeModel;
import com.bridgeintelligent.tag.webserver.system.service.NoticeService;
import com.wayneleo.quickstart.QuickException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


@Api(tags = "公告管理")
@AllArgsConstructor
@RestController
@RequestMapping("/api/notice")
@Slf4j
public class NoticeController {

    private NoticeService service;


    @ApiOperation(value = "新增或者编辑公告",notes = "公告管理")
    @PostMapping("/addOrUpd")
    public Resp addOrUpd(@RequestBody NoticeModel model) {
        try {
            //校验公告长度
            if (model.getTitle().length() > 50) {
                throw QuickException.normal(SystemErrors.ERROR_701004);
            }
            service.addOrUpd(model);
            return new Resp(PublicConstant.SUCCESS);
        } catch (Exception e) {
            return new Resp(PublicConstant.FAIL, e.getMessage());
        }
    }


    @ApiOperation(value = "公告状态变更操作",notes = "公告管理")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id"),
            @ApiImplicitParam(paramType = "query", name = "status", value = "状态（0未开启 1启用 2下架 3删除）"),
    })
    @PostMapping("/operate")
    public Resp operate(@RequestBody NoticeModel model) {
        try {
            service.operate(model);
            return new Resp(PublicConstant.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Resp(PublicConstant.FAIL,e.getMessage());
        }
    }


    @ApiOperation("查看单个公告信息")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "id")
    })
    @GetMapping("/findOne")
    public Resp findOne(String id) {
        try {
            return new Resp(PublicConstant.SUCCESS, service.findOneById(id));
        }catch (Exception e){
            e.printStackTrace();
            return new Resp(PublicConstant.FAIL);
        }
    }


    @ApiOperation("获取公告列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNumber", value = "页码"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "条数"),
            @ApiImplicitParam(paramType = "query", name = "title", value = "组合标签名称"),
    })
    @GetMapping("/list")
    public Resp list(@ModelAttribute NoticeModel model) {
        return new Resp(PublicConstant.SUCCESS,service.list(model));
    }

    @ApiOperation("获取当前登录人查看最新公告的信息")
    @GetMapping("/findUserNotice")
    public Resp findUserNotice() {
        return new Resp(PublicConstant.SUCCESS,service.findUserNotice());
    }

}